package indi.zhang.common.branchs;

import java.util.List;

public class AbstraceHandle<T, R>{
    //处理beans容器
    private List<Handle> handleList;

    public void setHandleList(List<Handle> handleList) {
        this.handleList = handleList;
    }

    //执行业务逻辑
    public R execute(T t) {
        //获取逻辑处理bean
        Handle handle = getHandle(t);
        if (handle == null) {
            throw new RuntimeException("can not find handle");
        }
        return (R) handle.execute(t);
    }


    //具体业务bean选择
    private Handle getHandle(T t) {
        if (handleList == null) {
            return null;
        }
        return handleList.stream().filter(h -> h.accpet(t)).findFirst().orElse(null);
    }
}
